package org.fanrui.hrm.web.controller;

import com.baomidou.mybatisplus.plugins.Page;
import org.fanrui.hrm.domain.CourseType;
import org.fanrui.hrm.exception.GlobalException;
import org.fanrui.hrm.query.CourseTypeQuery;
import org.fanrui.hrm.service.ICourseTypeService;
import org.fanrui.hrm.util.AjaxResult;
import org.fanrui.hrm.util.PageList;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 课程类型管理
 *
 * @author fanrui
 */
@RestController
@RequestMapping("/courseType")
public class CourseTypeController {
    @Resource
    public ICourseTypeService courseTypeService;

    /**
     * 获取课程类型目录树形结构
     *
     * @return 课程类型目录树形结构
     */
    @GetMapping("/treeData/{id}")
    public AjaxResult getTreeData(@PathVariable("id") Long id) {
        try {
            List<CourseType> treedData = courseTypeService.treeData(id);
            return AjaxResult.me().setSuccess(true).setResultObj(treedData);
        } catch (GlobalException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }
    }

    /**
     * 保存和修改公用的
     *
     * @param courseType 传递的实体
     * @return Ajaxresult转换结果
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public AjaxResult save(@RequestBody CourseType courseType) {
        try {
            if (courseType.getId() != null) {
                courseTypeService.updateById(courseType);
            } else {
                courseTypeService.insert(courseType);
            }
            return AjaxResult.me();
        } catch (GlobalException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！" + e.getMessage());
        }
    }

    /**
     * 删除对象信息
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
            courseTypeService.deleteById(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除对象失败！" + e.getMessage());
        }
    }

    //获取用户
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public CourseType get(@PathVariable("id") Long id) {
        return courseTypeService.selectById(id);
    }


    /**
     * 查看所有的员工信息
     *
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public List<CourseType> list() {

        return courseTypeService.selectList(null);
    }


    /**
     * 分页查询数据
     *
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @RequestMapping(value = "/pagelist", method = RequestMethod.POST)
    public PageList<CourseType> json(@RequestBody CourseTypeQuery query) {
        Page<CourseType> page = new Page<CourseType>(query.getPage(), query.getRows());
        page = courseTypeService.selectPage(page);
        return new PageList<CourseType>(page.getTotal(), page.getRecords());
    }
}
